<?php

namespace App\Http\Controllers\Stock;

use App\Http\Controllers\BaseController;
use App\Models\Order\Saleordersub;
use App\Models\Order\Salesordersub;
use App\Models\Purchase\PurchaseOrderSub;
use App\Models\Purchase\PurchaseStockinOrderSub;
use App\Models\Steel\SteelMaterialPriceSupply;
use App\Models\Stock\Stock;
use App\Models\Stock\StockAupplyAddPriceLog;
use App\Services\Stock\Stock as StockStock;
use Illuminate\Http\Request;

/**
 * 库存管理
 */
class StockController extends BaseController
{

    /**
     * 库存列表
     *
     * @param Request $request
     * @return void
     */
    public function getlist(Request $request)
    {
        // echo round("0.8999967599792639",4);exit;
        // echo mathfunc('div',1.1111,1.23456,4,1);exit;

        // echo mathfunc('add',1.11,1.23,4,0);exit;
        $list = StockStock::getList($request, $this->mangerinfo['cpid'], $this->mangerinfo['utype']);

        if(isset($list['list'])){
            $config_arr = config('steelconfig.steel_type_2');
            foreach($list['list'] as &$val){
                if(isset($val['archive_material'])){
                    if (in_array($val['archive_material'], $config_arr)) {
                        $val['is_red'] = 2;
                    } else {
                        $val['is_red'] = 1;
                    }
                }

            }
        }

        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }

    public function topdata(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];
        $can_use_weight = Stock::where('cpid', $cpid)->sum('can_use_weight');
        $porderweight = PurchaseOrderSub::where('cpid', $cpid)->where('is_del', 1)->sum('total_weight') - PurchaseStockinOrderSub::where('cpid', $cpid)->where('is_del', 1)->sum('total_weight');
        $sorderweight = Saleordersub::where('cpid', $cpid)->where('is_del', 1)->sum('total_weight') - Salesordersub::where('cpid', $cpid)->where('is_del', 1)->sum('weight');
        $real_can_use_weight = $can_use_weight - $sorderweight + $porderweight;

        return json_encode(['code' => '000', 'msg' => '获取成功', 'data' => ['can_use_weight' => $can_use_weight, 'porderweight' => $porderweight, 'sorderweight' => $sorderweight, 'real_can_use_weight' => $real_can_use_weight]]);
    }

    /**
     * 采购获取点价
     *
     * @param Request $request
     * @return void
     */
    public function getpointpricebypurchase(Request $request) {}

    /**
     * 采购配置存库查询加价
     *
     * @param Request $request
     * @return void
     */
    public function changesuppleyaddprciebypurchase(Request $request)
    {

        $stock_id = $request->stock_id ?? 0; //库存记录主键

        $info = Stock::where(['cpid' => $this->mangerinfo['cpid'], 'id' => $stock_id])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        // $add_price = intval(bcmul(($request->add_price ?? 0), 100, 0)); //加价
        $add_price = intval($request->add_price); //加价

        if ($add_price < 0) {
            return response()->json(['code' => '001', 'msg' => '加价不能小于0']);
        }


        Stock::where(['cpid' => $this->mangerinfo['cpid'], 'id' => $stock_id])->update(['supply_add_price' => $add_price]);
        $data = [
            'cpid' => $this->mangerinfo['cpid'],
            'stock_id' => $stock_id,
            'supply_add_price_old' => $info->supply_add_price,
            'supply_add_price_new' => $add_price,
            'create_at' => time(),
            'manger_id' => $this->mangerinfo['id']
        ];
        StockAupplyAddPriceLog::create($data);

        return response()->json(['code' => '000', 'msg' => '修改加价成功']);
    }
}
